package com.zimmur.tools;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.annotate.JsonIgnore;

public class PageSizeUtil<E> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//当前页  
    private int currentPage = 1;  
    //每页显示数量  
    private int pageSize = 10;  
      
    //总页数  
    private int totalPage=0;
    
    //记录总条数
    private int rowsCount=0;
    @JsonIgnore
    private int startRowsIndex=0;
    
    private List<E> pageList;
    
    public PageSizeUtil(){  
        
    }  
      
    /**
     * 初始参数
     * @param totalCount 总记录数
     * @param pageSize 每页记录数
     * @param currentPage 当前页
     * @param navigatorSize 
     */
    public PageSizeUtil(int rowsCount, int pageSize, int currentPage) {  
        this.rowsCount = rowsCount;  
        this.pageSize = pageSize;  
        this.currentPage = currentPage; 
    } 
    
    public int getTotalPage() {  
    	totalPage = 0;  
        if (pageSize != 0) {  
        	totalPage = rowsCount / pageSize;  
            if (rowsCount % pageSize != 0)  
            	totalPage++;  
        }  
  
        return totalPage;  
    } 
    /**
     * 获取当前页
     * @return
     */
    public int getCurrentPage() {  
//        currentPage = currentPage < getTotalPage() ? currentPage : getTotalPage();  
        currentPage = currentPage < 1 ? 1 : currentPage;  
  
        return currentPage;  
    }  
    public void setCurrentPage(int currentPage) {  
        this.currentPage = currentPage;  
    }  
    
    public int getPageSize() {  
        return pageSize;  
    }  
  
    public void setPageSize(int pageSize) {  
        this.pageSize = pageSize;  
    }  
    
    public int getRowsCount() {  
        return rowsCount;  
    } 
    
    public void setRowsCount(int rowsCount) {  
        this.rowsCount = rowsCount;  
    }  
    /**
     * 获取起始记录位置
     * @return
     */
    public int getStartRowsIndex(){
    	return (getCurrentPage()-1)*pageSize;
    }  
    public List<E> getPageList() {
		return pageList==null?new ArrayList<E>():pageList;
	}

	public void setPageList(List<E> pageList) {
		this.pageList = pageList;
	}


	public static void main(String[] args) {
		PageSizeUtil pageUtil = new PageSizeUtil(100, 10, 1);
		
		pageUtil.setCurrentPage(3);
		
		System.out.println(pageUtil);
		
		pageUtil.setCurrentPage(5);
		System.out.println(pageUtil);
		
		//超过总页数
		pageUtil.setCurrentPage(15);
		System.out.println(pageUtil);
	}
	
}
